﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;

namespace ExpertMIS.UserControl
{
    public partial class ucWebRoad : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            smpRoad.Provider.SiteMapResolve += new SiteMapResolveEventHandler(Provider_SiteMapResolve);
        }

        private ADMColumnEntity GetArticleColumn(string ArticleOID)
        {
            ADMColumnBroker broker = new ADMColumnBroker();
            return broker.GetColumnByArtileOID(ArticleOID);
        }

        private DomainEntity GetExpertDomain(string ExpertOID)
        {
            DomainBroker domainBroker = new DomainBroker();
            foreach (DomainEntity domain in domainBroker.GetExpertGoodAtDomains(ExpertOID))
            {
                return domain;
            }

            return null;
        }

        private DomainEntity GetQuestionDomain(string QuestionOID)
        {
            DomainBroker domainBroker = new DomainBroker();
            foreach (DomainEntity domain in domainBroker.GetDomainsByQuerytionOID(QuestionOID))
            {
                return domain;
            }

            return null;
        }

        private BulletinEntity GetBulletin(string BulletinOID)
        {
            BulletinBroker bulletinBroker = new BulletinBroker();
            return bulletinBroker.GetBulletinByOID(BulletinOID);
        }

        SiteMapNode Provider_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            if (SiteMap.CurrentNode == null) return null;

            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tempNode = currentNode;

            if (tempNode.Key.EndsWith("/forums/expertview.aspx"))
            {
                string ExpertOID = e.Context.Request.QueryString["oid"];
                DomainEntity domain = GetExpertDomain(ExpertOID);

                if (domain != null)
                {
                    tempNode.ParentNode.Url = "~/Forums/ExpertList.aspx?oid=" + domain.OID;

                    tempNode.ParentNode.ParentNode.Url = "~/Forums/DomainIntroduce.aspx?oid=" + domain.OID;
                    tempNode.ParentNode.ParentNode.Title = domain.DomainName;
                }
            }
            else if (tempNode.Key.EndsWith("/forums/domainintroduce.aspx"))
            {
                string DomainOID = e.Context.Request.QueryString["oid"];
                DomainBroker domainBroker = new DomainBroker();
                DomainEntity domain = domainBroker.GetDomainByOID(DomainOID);

                if (domain != null)
                {
                    tempNode.Title = domain.DomainName;
                }
            }
            else if (tempNode.Key.EndsWith("/forums/questionform.aspx"))
            {
                string QuestionOID = e.Context.Request.QueryString["oid"];
                DomainEntity domain = GetQuestionDomain(QuestionOID);

                if (domain != null)
                {
                    tempNode.ParentNode.Url = "~/Forums/ExpertList.aspx?oid=" + domain.OID;
                    tempNode.ParentNode.Title = domain.DomainName;
                }
            }
            else if (tempNode.Key.EndsWith("/forums/expertlist.aspx"))
            {
                string DomainOID = e.Context.Request.QueryString["oid"];

                DomainBroker domainBroker = new DomainBroker();
                DomainEntity domain = domainBroker.GetDomainByOID(DomainOID);

                if (domain != null)
                {
                    tempNode.ParentNode.Url = "~/Forums/DomainIntroduce.aspx?oid=" + domain.OID;
                    tempNode.ParentNode.Title = domain.DomainName;
                }
            }
            else if (tempNode.Key.EndsWith("/columns/bulletinlist.aspx"))
            {
                string catalog = e.Context.Request.QueryString["catalog"];

                if (catalog == "1")
                {
                    tempNode.ParentNode.Title = "组织服务人才";
                    tempNode.ParentNode.Url = "~/OrganizationService.aspx";
                }
                else if (catalog == "2")
                {
                    tempNode.ParentNode.Title = "人才服务社会";
                    tempNode.ParentNode.Url = "~/SocialService.aspx";
                }
                else
                {
                    tempNode.ParentNode.Title = "未知";
                    tempNode.ParentNode.Url = "";
                }
            }
            else if (tempNode.Key.EndsWith("/columns/viewbulletin.aspx"))
            {
                BulletinEntity bulletin = GetBulletin(e.Context.Request.QueryString["oid"]);

                if (bulletin != null)
                {
                    if (bulletin.BulletinZone == "Org")
                    {
                        tempNode.ParentNode.Url = "~/Columns/BulletinList.aspx?catalog=1";
                        tempNode.ParentNode.ParentNode.Title = "组织服务人才";
                        tempNode.ParentNode.ParentNode.Url = "~/OrganizationService.aspx";
                    }
                    else if (bulletin.BulletinZone == "Talent")
                    {
                        tempNode.ParentNode.Url = "~/Columns/BulletinList.aspx?catalog=2";
                        tempNode.ParentNode.ParentNode.Title = "人才服务社会";
                        tempNode.ParentNode.ParentNode.Url = "~/SocialService.aspx";
                    }
                    else
                    {
                        tempNode.ParentNode.Title = "未知";
                        tempNode.ParentNode.ParentNode.Title = "未知";
                    }
                }
            }
            else if (tempNode.Key.EndsWith("/columns/columnview.aspx"))
            {
                string ColumnOID = e.Context.Request["oid"];
                ADMColumnBroker columnBroker = new ADMColumnBroker();
                ADMColumnEntity column = columnBroker.GetColumnEntityByOID(ColumnOID);

                if (column != null)
                {
                    tempNode.Title = column.Name;
                    tempNode.Url = "~/Columns/ColumnView.aspx?oid=" + ColumnOID;
                }
            }
            else if (tempNode.Key.EndsWith("/columns/article.aspx"))
            {
                string ArticleOID = e.Context.Request["oid"];
                ADMColumnEntity column = GetArticleColumn(ArticleOID);

                if (column != null)
                {
                    tempNode.ParentNode.Title = column.Name;
                    tempNode.ParentNode.Url = "~/Columns/ColumnView.aspx?oid=" + column.OID;
                }
            }
            else if (tempNode.Key.EndsWith("/forums/domainfaq.aspx"))
            {
                string DomainOID = e.Context.Request.QueryString["oid"];

                DomainBroker domainBroker = new DomainBroker();
                DomainEntity domain = domainBroker.GetDomainByOID(DomainOID);

                if (domain != null)
                {
                    tempNode.ParentNode.Url = "~/Forums/DomainIntroduce.aspx?oid=" + domain.OID;
                    tempNode.ParentNode.Title = domain.DomainName;
                }
            }

            return tempNode;
        }
    }
}